-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (25 loc) · 768 Bytes
/
Solution.java
File metadata and controls
32 lines (25 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class FibonacciSeries {
static void fibonacci(int n) {
int first = 0, second = 1, next;
System.out.print("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
System.out.print(first + " ");
next = first + second;
first = second;
second = next;
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = scanner.nextInt();
if (n <= 0) {
System.out.println("Please enter a positive integer.");
} else {
fibonacci(n);
}
scanner.close();
}
}